<!-- 作者:唐嘉诚 -->
<!-- 添加院线 -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title></title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <style>
        .imgs{
            margin-left: 110px;
            margin-top: 10px;
            position: relative;
            width:150px;
            height:150px;
            display: none;
        }
        .imgs i{
            position: absolute;
            right: 2px;
            top: 2px;
            color:#fff;
            z-index:1;
            cursor: pointer;
        }
        .imgs::after{
            content: "";
            position:absolute;
            height:20px;
            background:rgba(0,0,0,.5);
            top:0;
            left:0;
            width:100%;
            border-radius: 5px 5px 0 0;

        }
        .imgs img{
            width:100%;
            height:100%;
            display: block;
            object-fit: contain;
            border-radius:5px;
            border: 1px solid gray;
            box-sizing: border-box;
        }
    </style>
</head>
<body class="layui-layout-body">
    <div class="layui-layout layui-layout-admin">
        <div class="layui-body" style="position:absolute;left:50px;top:40px;">
            <button class="layui-btn layui-btn-sm" onclick="location.href = 'index.html'">返回</button>
            <div style="padding: 15px;">
                <form class="layui-form" lay-filter="addForm" action="" onsubmit="return false">
                    <div class="layui-form-item">
                        <label class="layui-form-label">院线名称</label>
                        <div class="layui-input-inline">
                            <input type="text" style="width:400px" name="name" lay-verify="required" placeholder="请输入院线名称"
                                autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">院线地址</label>
                        <div class="layui-input-inline">
                            <input type="text" style="width:600px" name="addr" lay-verify="required" placeholder="请输入院线地址"
                                autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">图片</label>
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn" id="test1">
                                <i class="layui-icon">&#xe67c;</i>上传图片
                            </button>
                            <div class="imgs" style="margin-left:0px">
                                <img src="" alt="">
                                <i class="layui-icon layui-icon-close"></i>
                            </div>
                            <div class="img-input" style="display:none">
                                <input type="text" id="txtimg" name="cover">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">联系电话</label>
                        <div class="layui-input-inline">
                            <input type="text" name="tel" lay-verify="required|number" placeholder="请输入联系电话"
                                autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn" lay-submit lay-filter="cinema-add">添加院线</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.12.4.min.js"></script>
    <script>
        layui.use(['form', 'layer'], function () {
            const form = layui.form;
            form.on('submit(cinema-add)', async function (data) {
                delete data.field.imgfile;
                if (!data.field.cover) {
                    layui.layer.msg("请上传图片", {
                        icon: 2
                    });
                    return;
                }
                data.field.image=data.field.cover;
                await $.post({
                    url:"/api/Cinema", 
                    data:data.field,
                    success:function(){
                        layer.msg('添加院线成功', {
                            icon: 1,
                            time: 1000
                        }, function () {
                            location.href = "index.html";
                        });
                    },
                    error:function(){
                        layer.msg('添加失败,请检查后再试试', {
                            icon: 2,
                            time: 1000
                        });
                    }
                });
            });
        });

        layui.use('upload', function () {
            var upload = layui.upload;
            //执行实例
            var uploadInst = upload.render({
                elem: '#test1', //绑定元素
                url: '/api/upload', //上传接口
                field: "imgfile",
                acceptMime: "image/*",
                size: 500,
                multiple: true,
                drag: true,
                done: function (res) {
                    //上传完毕回调
                    $(".imgs img").prop("src", res.data);
                    $("#txtimg").val(res.data[0]);
                    $(".imgs").show();
                }
                , error: function () {
                    //请求异常回调
                    console.log("错误");
                }
            });
        });

        $(".imgs i").click(function () {
            $(".imgs img").prop("src", "");
            $("#txtimg").val("");
            $(".imgs").hide();
        })
    </script>
</body>
</html>